package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityAmericanComicBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageToonFilter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class AmericanComicActivity extends BaseAc<ActivityAmericanComicBinding> {

    public static String imgPath;
    private boolean hasOpenSpecialEffect;

    @Override
    protected int onCreate() {
        return R.layout.activity_american_comic;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAmericanComicBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivReplacePicture.setOnClickListener(this);
        mDataBinding.ivSavePicture.setOnClickListener(this);

        mDataBinding.sbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                GPUImageToonFilter filter = (GPUImageToonFilter) mDataBinding.gpuImgView.getFilter();
                filter.setLineSize(calLineSize(progress));
                mDataBinding.gpuImgView.requestRender();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        mDataBinding.ivImgCompare.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!hasOpenSpecialEffect) return true;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mDataBinding.ivAmericanComicImg.setVisibility(View.VISIBLE);
                        mDataBinding.gpuImgView.setVisibility(View.GONE);
                        Glide.with(mContext).load(imgPath).into(mDataBinding.ivAmericanComicImg);
                        break;
                    case MotionEvent.ACTION_UP:
                        mDataBinding.ivAmericanComicImg.setVisibility(View.GONE);
                        mDataBinding.gpuImgView.setVisibility(View.VISIBLE);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });

    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivReplacePicture://切换图片
                Intent intent = new Intent(this, SelPicActivity.class);
                intent.putExtra(Content.Replace, "Replace");
                startActivity(intent);
                break;
            case R.id.ivSavePicture://保存图片
                saveCartoon();
                break;
        }
    }

    @Override
    protected void initData() {
        cartoon();
    }

    private void saveCartoon() {
        showDialog(getString(R.string.save_ing));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Bitmap bitmap = null;
                try {
                    bitmap = mDataBinding.gpuImgView.capture();
                    emitter.onNext(bitmap);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    emitter.onNext(bitmap);
                }
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                String path = FileUtil.generateFilePath(Content.myRecord, ".png");
                ImageUtils.save(bitmap, path, Bitmap.CompressFormat.PNG);
                ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                ToastUtils.showShort(getString(R.string.save_success));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(HomeActivity.class);
                    }
                }, 500);
            }
        });
    }

    private void cartoon() {
        mDataBinding.ivAmericanComicImg.setVisibility(View.GONE);
        mDataBinding.gpuImgView.setVisibility(View.VISIBLE);
        mDataBinding.gpuImgView.setImage(FileUtils.getFileByPath(imgPath));
        mDataBinding.gpuImgView.setFilter(new GPUImageToonFilter());
        hasOpenSpecialEffect = true;
    }

    private float calLineSize(int progress) {
        float start = 0f;
        float end = 5.0f;
        return (end - start) * progress / 100f + start;
    }

}